package com.xbfinance.portal.web;

import com.xbfinance.portal.pojo.po.P2pUser;
import com.xbfinance.portal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/name/{username}")
    @ResponseBody
    public Map<String, Object> isNameExist(@PathVariable String username){
        Map<String, Object> existFlag = new HashMap<>();
        boolean flag=userService.isNameExist(username);
        existFlag.put("flag",flag);
        return existFlag;
    }
    @GetMapping("/email")
    @ResponseBody
    public Map<String, Object> isEmailExist(String email){
        Map<String, Object> existFlag = new HashMap<>();
        boolean flag=userService.isEmailExist(email);
        existFlag.put("flag",flag);
        return existFlag;
    }
    @PostMapping("/regist")
    public String regist(P2pUser p2pUser, HttpSession session, HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        try {
            p2pUser=userService.regist(p2pUser,ip);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(p2pUser!=null){
            session.setAttribute("user",p2pUser);
            return "regist_yz_mail";
        }
        return "regist";
    }
    @PostMapping("/smrz")
    public String userinfo(String realname,String cardnum,Integer userid,Model model){
        boolean flag=false;
        try {
            flag=userService.putInfo(realname,cardnum,userid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag) {
            return "redirect:/index";
        }else{
            model.addAttribute("userid",userid);
            model.addAttribute("errorMsg","实名认证错误");
            return "regist_success";
        }
    }

    @RequestMapping("/sendCheckEmail")
    public String sendCheckEmail(HttpSession session, HttpServletRequest request){
        P2pUser user = (P2pUser) session.getAttribute("user");
        try {
            userService.sendCheckEmail(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "regist_yz_mail_qr";
    }
    @RequestMapping("/checkEmailPhone/{userid}")
    public String checkEmailPhone(@PathVariable Integer userid, Model model){
        try {
            userService.checkEmailPhone(userid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("userid", userid);
        return "regist_success";
    }
    @PostMapping("/login")
    public String login(String email, String loginpass, Model model, HttpSession session){
        P2pUser user=null;
        try {
            user = userService.login(email, loginpass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(user!=null){
            session.setAttribute("user",user);
            return "redirect:/index";
        }else{
            model.addAttribute("errorMsg", "登入名或者密码错误");
            return "regist";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }
}
